package com.cz.zslu.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.cz.zslu.common.ReturnObject;
import com.cz.zslu.entity.FeedBack;
import com.cz.zslu.entity.OnlineIM;
import com.cz.zslu.entity.SysUser;
import com.cz.zslu.entity.ZslStore;
import com.cz.zslu.interceptor.BaseContextHandler;
import com.cz.zslu.interceptor.IgnoreAuth;
import com.cz.zslu.rest.BaseController;
import com.cz.zslu.service.FeedBackService;
import com.cz.zslu.service.ISysUserService;
import com.cz.zslu.service.IZslStoreService;
import com.cz.zslu.service.OnlineIMService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 意见反馈 前端控制器
 * </p>
 *
 * @author Bruce
 * @since 2019-06-05
 */
@RestController
@RequestMapping("/sys/feedback")
@Api(description = "意见反馈管理")
public class FeedBackController extends BaseController<FeedBackService, FeedBack> {


    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private OnlineIMService onlineIMService;

    @Autowired
    private IZslStoreService zslStoreService;





    @ApiOperation("通过id获取详情")
    @RequestMapping(value = "/findInfoById", method = RequestMethod.POST)
    public ReturnObject<FeedBack> findInfoById(@ApiParam("id") @RequestParam Long id) {

        return new ReturnObject<>(0, "获取成功", baseService.selectById(id));
    }


    @ApiOperation("获取全部意见反馈分页")
//    @IgnoreAuth
    @RequestMapping(value = "/getPage", method = RequestMethod.POST)
    public ReturnObject<List<FeedBack>> getPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                @ApiParam("名字/内容") @RequestParam  String name,
                                                @ApiParam("是否处理 0、未处理 1、已处理") @RequestParam  Integer isDeal) {
        EntityWrapper<FeedBack> ew = new EntityWrapper<>();
        if(isDeal!=null){
            ew.eq("is_deal", isDeal);
        }
        ew.eq("state", 0);
        if (StringUtils.isNotBlank(name)) {
            ew.andNew().like("username", name).or().like("content", name);
        }
        ew.orderBy("crt_time", false);
        Page<FeedBack> obj = baseService.selectPage(new Page<>(offset, limit), ew);
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


    @ApiOperation("新增")
    @RequestMapping(value = "/saveFeedBack", method = RequestMethod.POST)
    public ReturnObject<FeedBack> saveFeedBack(@ApiParam("图片") @RequestParam String picUrl,
                                               @ApiParam("内容") @RequestParam String content,
                                               @ApiParam("意见反馈") @RequestParam String opinion) {

        SysUser sysUser = iSysUserService.selectById(BaseContextHandler.getUserID());
        FeedBack info = new FeedBack();
        info.setContent(content);
        info.setOpinion(opinion);
        info.setPicUrl(picUrl);
        info.setUserId(sysUser.getId());
        info.setUsername(sysUser.getUsername());
        info.setPhone(sysUser.getPhone());
        baseService.insert(info);
        return new ReturnObject<>(0, "新增成功", null);
    }

    @ApiOperation("处理")
    @RequestMapping(value = "/deal", method = RequestMethod.POST)
    public ReturnObject<FeedBack> deal(@ApiParam("id") @RequestParam Long id,
                                       @ApiParam("处理意见") @RequestParam String dealOpinion) {
        FeedBack info = baseService.selectById(id);
        info.setIsDeal(1);
        info.setDealOpinion(dealOpinion);
        baseService.updateById(info);
        //反馈用户
        SysUser toUser = iSysUserService.selectById(info.getUserId());
        //系统管理员
        SysUser fromSysUser = iSysUserService.selectById(BaseContextHandler.getUserID());

        OnlineIM onlineIM = new OnlineIM();
        onlineIM.setType(7);
        onlineIM.setName(fromSysUser.getUsername());
        onlineIM.setFromUserId(fromSysUser.getId());
        onlineIM.setHeadPic(fromSysUser.getHeadPortraits());
        onlineIM.setPhone(fromSysUser.getPhone());
        onlineIM.setContent(dealOpinion);
        onlineIM.setIsDeal(1);
        onlineIM.setStoreId(toUser.getStoreId());
        onlineIM.setUserId(info.getUserId());
        ZslStore store = zslStoreService.selectById(toUser.getStoreId());
        onlineIM.setStoreName(store.getName());
        onlineIMService.insert(onlineIM);
        return new ReturnObject<>(0, "处理成功");
    }

    @ApiOperation("删除")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject<FeedBack> del(@ApiParam("id") @RequestParam Long id) {
        FeedBack info = baseService.selectById(id);
        info.setState(1);
        baseService.updateById(info);
        return new ReturnObject<>(0, "删除成功");
    }

}

